define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/category/index' + location.search,
                    add_url: 'goods/category/add',
                    edit_url: 'goods/category/edit',
                    del_url: 'goods/category/del',
                    // dragsort_url:'',
                    // multi_url: 'goods/category/multi',
                    // import_url: 'goods/category/import',
                    table: 'goodscate',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                searchFormVisible:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'name', title: '分类名称', operate: false, align: 'left', formatter:function (value, row, index) {
                                return value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
                            }
                        },
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        // {field: 'pid', title: __('Pid'), operate: false},
                        // {field: 'weigh', title: __('Weigh'), operate: false},
                        //{field: 'addtime', title: __('Addtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            //根据当前屏幕高度，实时调整table表格高度，让他能出现滚动条
            $(document).ready(function() {
                // 初始化时获取屏幕高度
                var screenHeight = $(window).height();
                //console.log("屏幕高度: " + screenHeight);
                    var searchHeigh = $(".commonsearch-table").height();

                    //console.log(screenHeight,searchHeigh);
                    //有顶部tab时候多减57
                    var thisHeight = screenHeight-searchHeigh - 200;

                    //console.log("1屏幕高度已改变: " + thisHeight);

                    $(".fixed-table-body").css("height",thisHeight);

             
                // 监听窗口大小改变事件
                $(window).resize(function() {
                    // 当窗口大小改变时，重新获取屏幕高度
                    var newScreenHeight = $(window).height();
                    // var searchHeigh = 300;
                    var searchHeigh = $(".commonsearch-table").height();

                    //console.log(newScreenHeight,searchHeigh);
                    var thisHeight = newScreenHeight-searchHeigh - 200;
                    //console.log("2屏幕高度已改变: " + thisHeight);

                    $(".fixed-table-body").css("height",thisHeight);
                });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {

            $(document).on("change", "#c-pid", function(){
                var pcategoryid = $(this).val();
                if(pcategoryid > 0)
                {
                    $(".cateimg").hide();
                }
                else
                {
                    $(".cateimg").show();
                }
            });
            //不验证不可见表单
            $('form[role=form]').data("validator-options", {
                ignore: ':hidden'
            });

            Controller.api.bindevent();
        },
        edit: function () {

            var pid = $("#c-pid").val();
            if (pid > 0) {
                $(".cateimg").hide();
            }
            
            $(document).on("change", "#c-pid", function(){
                var pcategoryid = $(this).val();
                if(pcategoryid > 0)
                {
                    $(".cateimg").hide();
                }
                else
                {
                    $(".cateimg").show();
                }
            });
            //不验证不可见表单
            $('form[role=form]').data("validator-options", {
                ignore: ':hidden'
            });
            
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
